package com.xiaoka.ins.corp.api.aics.dto.config;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author donggua
 * 永诚配置信息
 */
public class StaticConfig {
	
	/**
	 * 险别配置
	 */
	public static List<Risk> risks;
	/**
	 * 区域配置
	 */
	public static List<Region> regions;
	
	static {
		
		URL url = StaticConfig.class.getClassLoader().getResource("config/company/aics.json");
		String json = "";
		try {
			json = IOUtils.toString(url, "utf-8").replaceAll("\\s*", "");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		ObjectMapper mapper = new ObjectMapper();
		JsonNode node = null;
		try {
			node = mapper.readTree(json);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		risks = readJson(node.get("risks").toString(), List.class, Risk.class); 
		regions = readJson(node.get("regions").toString(), List.class, Region.class);
	}
	
	/**
	 * 获取泛型的Collection Type
	 * @param jsonStr json字符串
	 * @param collectionClass 泛型的Collection
	 * @param elementClasses 元素类型
	 */
	private static <T> T readJson(String jsonStr, Class<?> collectionClass, Class<?>... elementClasses){ 
       ObjectMapper mapper = new ObjectMapper();
       JavaType javaType = mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
       try {
			return mapper.readValue(jsonStr, javaType);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}
